package com.zmn.plat.admin.controller.channel.cityratio;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.common.constant.ProductConsts;
import com.zmn.plat.model.vo.channel.ChannelCityPriceRatioVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zmn.common.constant.StatusConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.plat.business.interfaces.channel.ChannelCityPriceRatioBService;
import com.zmn.plat.common.dictionary.ChannelCityRatioTypeEnum;
import com.zmn.plat.model.entity.channel.ChannelCityPriceRatio;
import com.zmn.plat.model.entity.channel.ChannelCityPriceRatioQuery;
import com.zmn.plat.model.vo.channel.ChannelCityPriceRatioModifyVo;
import com.zmn.plat.model.vo.channel.ChannelCityRatioBatchVO;

/**
 * 类描述:渠道城市价格系数管理修改控制
 *
 * @author linfeng
 * @since 2019/08/27/10:02
 */
@Controller
@RequestMapping("/channel/citypriceratio/")
public class ChannelCityPriceRatioModifyController extends ChannelCityPriceRatioBaseController {
    private Logger logger = LoggerFactory.getLogger(ChannelCityPriceRatioModifyController.class);

    @Autowired
    private ChannelCityPriceRatioBService channelCityPriceRatioBService;

    /**
     * 新增/修改渠道城市默认系数
     *
     * @param request
     * @param channelCityPriceRatioVo
     * @return
     */
    @RequestMapping("addEdit")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody ChannelCityPriceRatioVo channelCityPriceRatioVo) {
        Date now = DateUtil.getNow();
        ChannelCityPriceRatioQuery channelCityPriceRatioQuery = new ChannelCityPriceRatioQuery();
        channelCityPriceRatioQuery.setChannelId(channelCityPriceRatioVo.getChannelId());
        channelCityPriceRatioQuery.setRatioType(ChannelCityRatioTypeEnum.CHANNELCITYRATIO.getCode());
        channelCityPriceRatioQuery.setPriceType(channelCityPriceRatioVo.getPriceType());
        channelCityPriceRatioQuery.setBizType(channelCityPriceRatioVo.getBizType());
        channelCityPriceRatioQuery.setCityId(channelCityPriceRatioVo.getCityId());
        ChannelCityPriceRatio queryResult = channelCityPriceRatioService.findByChannelIdAndCityAndCategAndType(channelCityPriceRatioQuery);
        try {
            Double toBeEffectiveRatio = channelCityPriceRatioVo.getToBeEffectiveRatio();
            String operator = getMcStaffRealName();

            Integer bizType = channelCityPriceRatioVo.getBizType();
            if (bizType == ProductConsts.BIZ_TYPE_C) {
                // C类业务价格系数时新增修改计价器3.0默认系数
                channelCityPriceRatioQuery.setBizType(null);
                channelCityPriceRatioQuery.setRatioType(ChannelCityRatioTypeEnum.CHANNELCITYMETER3RATIO.getCode());
                ChannelCityPriceRatio queryResultM = channelCityPriceRatioService.findByChannelIdAndCityAndCategAndType(channelCityPriceRatioQuery);
                Double meterToBeEffectiveRatio = channelCityPriceRatioVo.getMeterToBeEffectiveRatio();
                if (queryResultM == null) {
                    if (NumberUtil.isNotNullOrZero(meterToBeEffectiveRatio)) {
                        ChannelCityPriceRatio createOneRatioM = new ChannelCityPriceRatio();
                        BeanUtils.copyProperties(channelCityPriceRatioVo, createOneRatioM);
                        createOneRatioM.setToBeEffectiveRatio(meterToBeEffectiveRatio);
                        createOneRatioM.setCreater(operator);
                        createOneRatioM.setUpdater(operator);
                        createOneRatioM.setCreateTime(now);
                        createOneRatioM.setUpdateTime(now);
                        createOneRatioM.setBizType(null);
                        createOneRatioM.setRatioType(ChannelCityRatioTypeEnum.CHANNELCITYMETER3RATIO.getCode());
                        channelCityPriceRatioService.insert(createOneRatioM);
                    }
                } else {
                    if (NumberUtil.isNullOrZero(queryResultM.getRatio()) && NumberUtil.isNullOrZero(meterToBeEffectiveRatio)) {
                        channelCityPriceRatioService.deleteByKey(queryResultM.getRatioId());
                    } else {
                        queryResultM.setToBeEffectiveRatio(meterToBeEffectiveRatio);
                        queryResultM.setUpdateTime(DateUtil.getNow());
                        queryResultM.setUpdater(operator);
                        queryResultM.setEffectiveDate(channelCityPriceRatioVo.getEffectiveDate());
                        channelCityPriceRatioService.updateByKey(queryResultM);
                    }
                }
            }

            if (queryResult == null) {
                if (toBeEffectiveRatio != null) {
                    ChannelCityPriceRatio createOneRatio = new ChannelCityPriceRatio();
                    BeanUtils.copyProperties(channelCityPriceRatioVo, createOneRatio);
                    createOneRatio.setCreater(operator);
                    createOneRatio.setUpdater(operator);
                    createOneRatio.setCreateTime(now);
                    createOneRatio.setUpdateTime(now);
                    createOneRatio.setRatioType(ChannelCityRatioTypeEnum.CHANNELCITYRATIO.getCode());
                    channelCityPriceRatioService.insert(createOneRatio);
                }
            } else {
                if (queryResult.getRatio() == null && toBeEffectiveRatio == null) {
                    channelCityPriceRatioService.deleteByKey(queryResult.getRatioId());
                } else {
                    queryResult.setToBeEffectiveRatio(toBeEffectiveRatio);
                    queryResult.setUpdateTime(DateUtil.getNow());
                    queryResult.setUpdater(operator);
                    queryResult.setEffectiveDate(channelCityPriceRatioVo.getEffectiveDate());
                    channelCityPriceRatioService.updateByKey(queryResult);
                }
            }

            return ResponseDTO.success(StatusConsts.STATUS_SUCCESS, "操作成功！");
        } catch (org.springframework.dao.DuplicateKeyException ex) {
            logger.error("渠道城市价格系数【{}】已经存在，不能重复添加！", channelCityPriceRatioVo.getCityId());
            return ResponseDTO.fail("渠道城市价格系数已经存在，不能重复添加！");
        } catch (Exception ex) {
            logger.error("[zmn]新增/修改渠道城市价格系数失败,channelCityPriceRatio:{},ex:{}", channelCityPriceRatioVo, ex.getMessage(), ex);
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 添加或修改渠道城市保外分类系数
     *
     * @param request
     * @param channelCityPriceRatioModifyVo
     * @return
     */
    @RequestMapping("categoryRatioAddEdit")
    @ResponseBody
    public ResponseDTO addEditCategoryRatio(HttpServletRequest request,
                                            @RequestBody ChannelCityPriceRatioModifyVo channelCityPriceRatioModifyVo) {
        channelCityPriceRatioBService.addEditCategoryRatio(getMcStaffRealName(), channelCityPriceRatioModifyVo);
        return ResponseDTO.success(StatusConsts.STATUS_SUCCESS, "操作成功");
    }

    /**
     * 批量添加或修改城市保外系数
     *
     * @param request
     * @param cityRatioBatchVO
     * @return
     */
    @RequestMapping("saveCityRatioBatch")
    @ResponseBody
    public ResponseDTO saveCityOutRatioBatch(HttpServletRequest request, @RequestBody ChannelCityRatioBatchVO cityRatioBatchVO) {
        channelCityPriceRatioBService.saveCityRatioBatch(getMcStaff(), cityRatioBatchVO);
        return ResponseDTO.success(true, "设置成功");
    }

    /**
     * 批量添加或修改渠道城市分类系数
     *
     * @param request
     * @param cityRatioBatchVO
     * @return
     */
    @RequestMapping("saveCityCategRatioBatch")
    @ResponseBody
    public ResponseDTO saveCityCategRatioBatch(HttpServletRequest request, @RequestBody ChannelCityRatioBatchVO cityRatioBatchVO) {
        channelCityPriceRatioBService.saveCityCategRatioBatch(getMcStaff(), cityRatioBatchVO);
        return ResponseDTO.success(true, "设置成功");
    }
}
